'use strict';

const utility = require('utility');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const UserSchema = new Schema({
    loginname: {type: String},
    nickname: {type: String},
    pass: {type: String},
    email: {type: String},
    profile_image_url: {type: String},
    location: {type: String},
    signature: {type: String},
    profile: {type: String},
    url: {type: String}, // 主页地址
    weixin: {type: String},
    weibo: {type: String},
    avatar_url: {type: String},
    is_block: {type: Boolean, default: false},
    score: {type: Number, default: 0},
    topic_count: {type: Number, default: 0},
    reply_count: {type: Number, default: 0},
    collect_topic_count: {type: Number, default: 0},
    is_star: {type: Boolean},
    level: {type: String},
    active: {type: Boolean, default: false},
    receive_reply_mail: {type: Boolean, default: false},
    receive_at_mail: {type: Boolean, default: false},
    from_wp: {type: Boolean},
    retrieve_time: {type: Number},
    retrieve_key: {type: String},
    access_token: {type: String},
    auto_tags: {type: String}, // 智能收藏标签
    ignore_users: [], // 屏蔽不看的作者
    view_type: {type: Number, default: 1},  // 展示模式: 丰富(1), 简洁(0)
    followings: {type: Array}, // 你订阅的人
    fans: {type: Array}, // 你的粉丝
    last_create_topic: {type: Object}, // 最近一篇创建的文章信息
    froms: {type: Array}, // 用户来自于哪里
    default_boards: {type: Array}, // 用户录入文章时默认赋予的boards
    default_pic: {type: String}, // 用户录入文章时如果没有图片就默认赋予的图片
    is_admin: {type: Boolean, default: false},
    is_super_admin: {type: Boolean, default: false},
    last_click_tab_attention: {type: Date},
    isRobot: {type: Boolean, default: false}, // 是否是机器人用户
  }, {timestamps: true});

  UserSchema.index({loginname: 1}, {unique: true});
  UserSchema.index({email: 1}, {unique: true});
  UserSchema.index({score: -1});
  UserSchema.index({githubId: 1});
  UserSchema.index({access_token: 1});
  UserSchema.index({isRobot: 1}, {background: true});

  UserSchema.virtual('isAdvanced').get(function () {
    // 积分高于 700 则认为是高级用户
    return this.score > 700 || this.is_star;
  });

  // UserSchema.pre('save', function (next) {
  //   const now = new Date();
  //   this.updatedAt = now;
  //   next();
  // });

  return mongoose.model('User', UserSchema, 'users');
};
